/**
 * File    : AppModule.java
 * Created : 2008-9-5
 * By      : LuHuiguo
 */
package com.luhuiguo.sso.tutorial.services;

import java.io.IOException;

import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.ioc.OrderedConfiguration;
import org.apache.tapestry5.ioc.ServiceBinder;
import org.apache.tapestry5.ioc.annotations.InjectService;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.RequestFilter;
import org.apache.tapestry5.services.RequestGlobals;
import org.apache.tapestry5.services.RequestHandler;
import org.apache.tapestry5.services.Response;

/**
 * @author LuHuiguo
 * @version $Id: AppModule.java 2 2008-10-04 13:04:21Z LuHuiguo $
 * 
 */
public class AppModule {
	public static void bind(ServiceBinder binder) {
		// binder.bind(MyServiceInterface.class, MyServiceImpl.class);

		// Make bind() calls on the binder object to define most IoC services.
		// Use service builder methods (example below) when the implementation
		// is provided inline, or requires more initialization than simply
		// invoking the constructor.
	}

	public static void contributeApplicationDefaults(
			MappedConfiguration<String, String> configuration) {

		configuration.add("tapestry.supported-locales", "zh_CN,en");
		// configuration.add("tapestry.file-check-interval", "60000");
	}

	public void contributeRequestHandler(
			OrderedConfiguration<RequestFilter> configuration,
			@InjectService("Utf8Filter") final RequestFilter utf8Filter) {
		configuration.add("Utf8Filter", utf8Filter); // handle UTF-8

	}

	public RequestFilter buildUtf8Filter(
			@InjectService("RequestGlobals") final RequestGlobals requestGlobals) {
		return new RequestFilter() {
			public boolean service(Request request, Response response,
					RequestHandler handler) throws IOException {
				requestGlobals.getHTTPServletRequest().setCharacterEncoding(
						"UTF-8");
				return handler.service(request, response);
			}
		};
	}

}
